home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / TCPDevice.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  2.9 KB  |  129 lines

  1. // TCPDevice
  2. // The TCPDevice gives JFS client applets a way to make connections to
  3. // arbitrary hosts and ports.
  4. // An applet reading from /dev/TCP supplies a host and port, to which the
  5. // TCP device connects. If successful, the device starts a opens a
  6. // server socket on a random port, sends the port number back to the client,
  7. // then waits for a connection and ferrys all data to and fro between client
  8. // and the remote host.
  9. import java.io.*;
  10. import java.net.*;
  11.  
  12. public class TCPDevice extends DeviceDriver
  13. {
  14.     // read
  15.     // Valid message parameters are:
  16.     //  Host: <hostname>
  17.     //  Port: <port num>
  18.     Message read(Message msg, ServerClient sc) throws IOException
  19.     {
  20.     String host = msg.find("Host");
  21.     String port = msg.find("Port");
  22.     if (host == null)
  23.         throw new IOException("No Host given");
  24.     if (port == null)
  25.         throw new IOException("No Port given");
  26.  
  27.     // Make the connection
  28.     Socket s = null;
  29.     try s = new Socket(host, Integer.parseInt(port));
  30.     catch(UnknownHostException e)
  31.         throw new IOException("Host "+host+" not found");
  32.     catch(IOException e)
  33.         throw new IOException("Could not connect to "+host);
  34.  
  35.     // Find a free server socket
  36.     ServerSocket ss = null;
  37.     int p = 9888;
  38.     do {
  39.         p++;
  40.         try ss = new ServerSocket(p);
  41.         catch(Exception e);
  42.         } while(ss == null);
  43.  
  44.     // Start up a connection proxy on the port
  45.     new ConnectionProxy(s, ss);
  46.  
  47.     // Reply with the port
  48.     Message r = new Message();
  49.     r.add("Reply", "Success");
  50.     r.add("Port", String.valueOf(p));
  51.     return r;
  52.     }
  53. }
  54.  
  55. // ConnectionProxy
  56. // Given a socket and a server socket, wait for a connection and pass through
  57. // all data both ways
  58. class ConnectionProxy implements Runnable
  59. {
  60.     InputStream rin, cin;        // from remote host and client
  61.     OutputStream rout, cout;    // to remote host and client
  62.     ServerSocket sock;
  63.     Thread rth, cth;
  64.     
  65.     ConnectionProxy(Socket s, ServerSocket ss)
  66.     {
  67.     try {
  68.         cin = s.getInputStream();
  69.         cout = s.getOutputStream();
  70.         }
  71.     catch(Exception e)
  72.         return;        // huh?
  73.     sock = ss;
  74.     rth = new Thread(this, "Remote");
  75.     rth.start();
  76.     }
  77.  
  78.     public void run()
  79.     {
  80.     if (Thread.currentThread().getName().equals("Remote")) {
  81.         // Wait for a connection
  82.         Socket s = null;
  83.         try s = sock.accept();
  84.         catch(Exception e)
  85.             return;
  86.  
  87.         // Get the input and output streams from the new connection
  88.         try {
  89.             rin = s.getInputStream();
  90.             rout = s.getOutputStream();
  91.             }
  92.         catch(Exception e)
  93.             return;
  94.  
  95.         // Begin the client thread
  96.         cth = new Thread(this, "Client");
  97.         cth.start();
  98.  
  99.         // Read from the remote host, and write to the client
  100.         copy(rin, cout);
  101.         }
  102.     else {
  103.         // Read from the client, and write to the remote host
  104.         copy(cin, rout);
  105.         }
  106.     try {
  107.         rin.close();
  108.         rout.close();
  109.         cin.close();
  110.         cout.close();
  111.         }
  112.     catch(IOException e);
  113.     }
  114.  
  115.     // copy
  116.     // Copy all data from an input to an output, until EOF
  117.     void copy(InputStream in, OutputStream out)
  118.     {
  119.     try {
  120.         int c;
  121.         while((c = in.read()) != -1)
  122.             out.write((byte)c);
  123.         }
  124.     catch(IOException e)
  125.         return;
  126.     }
  127. }
  128.  
  129.